<?php

use Illuminate\Http\Response;
use Illuminate\Support\Arr;
use Laravel\Lumen\Http\ResponseFactory;

/**
 * 输出JSON响应
 * @param int $code 响应码
 * @param string $msg 提示信息
 * @param array $data 数据
 * @return Response|ResponseFactory
 */
function output(int $code, string $msg, array $data)
{
    $result = [
        'code' => $code,
        'msg'  => $msg,
        'data' => $data,
    ];
    return response($result);
}

/**
 * 请求成功
 * @param array $data 数据
 * @param string $msg 提示信息
 * @return Response|ResponseFactory
 */
function success(array $data = [], string $msg = 'success')
{
    return output(0, $msg, $data);
}

/**
 * 请求失败
 * @param string $msg 提示信息
 * @param int $code 错误码
 * @return Response|ResponseFactory
 */
function error($msg = 'fail', $code = 1001)
{
    return output($code, $msg, []);
}

/**
 * 生成分页HTML代码
 * @param int $count 数据总数
 * @param int $curr 当前页码
 * @param string $baseUrl 链接的URL
 * @param int $limit 每页显示的条数
 * @param int $groups 连续出现的页码个数
 * @return string 分页HTML代码
 */
function pagination(int $count, int $curr, int $limit = 10, int $groups = 5)
{
    $pages = ceil($count / $limit) ?: 1;
    $curr = $curr < 1 ? 1 : $curr;
    $curr = $curr > $pages ? $pages : $curr;

    $path = request()->path();
    $query = request()->query();

    if ($path != '/') {
        $path = '/' . $path;
    }

    $buildUrl = function ($page) use ($path, $query) {
        $query['page'] = $page;
        return $path . '?' . Arr::query($query);
    };

    if ($curr == 1) {
        $prev = '<a href="javascript:;" class="layui-laypage-prev layui-disabled">上一页</a>';
    } else {
        $prev = '<a href="' . $buildUrl($curr - 1) . '" class="layui-laypage-prev">上一页</a>';
    }

    if ($curr == $pages) {
        $next = '<a href="javascript:;" class="layui-laypage-next layui-disabled">下一页</a>';
    } else {
        $next = '<a href="' . $buildUrl($curr + 1) . '" class="layui-laypage-next">下一页</a>';
    }

    $start = 1;
    if ($curr >= $groups) {
        $start = $curr - intval($groups / 2);
    }

    $end = $start + $groups - 1;
    if ($end > $pages) {
        $start -= $end - $pages;
        $start = max($start, 1);
        $end = $pages;
    }

    $list = '';
    for ($i = $start; $i <= $end; $i++) {
        if ($i == $curr) {
            $list .= '<span class="layui-laypage-curr"><em class="layui-laypage-em"></em><em>' . $i . '</em></span>';
        } else {
            $list .= '<a href="' . $buildUrl($i) . '">' . $i . '</a>';
        }
    }

    $first = '';
    if ($start > 1) {
        $first = '<a href="' . $buildUrl(1) . '">1</a>';
        if ($start > 2) {
            $first .= '<span class="layui-laypage-spr">…</span>';
        }
    }

    $last = '';
    if ($end < $pages) {
        $last = '<a href="' . $buildUrl($pages) . '">' . $pages . '</a>';
        if ($end + 1 < $pages) {
            $last = '<span class="layui-laypage-spr">…</span>' . $last;
        }
    }

    $html = '<div class="layui-box layui-laypage layui-laypage-page" id="layui-laypage-1">'
        . $prev
        . $first
        . $list
        . $last
        . $next
        . '</div>';

    return $html;
}
